package com.changshi.video.service.impl;

import com.changshi.video.mapper.VideoMapper;
import com.changshi.video.service.VideoService;
import com.changshi.service.video.api.entity.Video;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName VideoServiceImpl
 * @Date 2020/1/2 20:08
 * @Author LeeSwag
 * @Verson 1.0
 **/

@Service
public class VideoServiceImpl implements VideoService {

    @Autowired
    private VideoMapper videoMapper;

    @Override
    public void add(Video video) {
        videoMapper.insert(video);
    }

    @Override
    public void delete(Long id) {
        videoMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Video video) {
        videoMapper.updateByPrimaryKeySelective(video);
    }

    @Override
    public List<Video> findVideoByUsername(String username) {
        Video video = new Video();
        video.setAuthor(username);
        return videoMapper.select(video);
    }

    @Override
    public Video findById(Long id) {
        Video video = videoMapper.selectByPrimaryKey(id);
        tagsToArray(video);
        return video;
    }

    @Override
    public List<Video> findAll() {
        List<Video> videos = videoMapper.findAll();
        for (Video video : videos) {
            tagsToArray(video);
        }
        return videos;
    }

    private void tagsToArray(Video video) {
        String tags = video.getTags();
        String[] tagList = tags.split(",");
        video.setTagList(tagList);
    }

    @Override
    public void playbackCountPlus(long videoId, int playbackCount) {
        Video video = new Video();
        video.setId(videoId);
        video.setPlaybackAmount(playbackCount);
        videoMapper.updateByPrimaryKeySelective(video);
    }

}
